package cn.itcast.controller.cargo;


import cn.itcast.controller.BaseController;
import cn.itcast.domain.cargo.Contract;
import cn.itcast.domain.cargo.Export;
import cn.itcast.domain.cargo.Invoice;
import cn.itcast.service.cargo.ContractService;
import cn.itcast.service.cargo.ExportService;
import cn.itcast.service.cargo.InvoiceService;
import cn.itcast.service.cargo.ShippingOrderService;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Date;

@Controller
@RequestMapping("/cargo/invoice")
public class InvoiceController extends BaseController {
    @Reference
    private InvoiceService invoiceService;
    @Reference
    private ShippingOrderService shippingOrderService;
    @Reference
    private ExportService exportService;
    @Reference
    private ContractService contractService;


    @RequestMapping(value = "/list", name = "展示发票单数据")
    public String findPage(@RequestParam(name = "page", defaultValue = "1") int pageNum, @RequestParam(defaultValue = "10") int size) {
        //查询装箱单数据
        PageInfo pageInfo = invoiceService.findAll(getCompanyId(), pageNum, size);
        request.setAttribute("page", pageInfo);
        return "cargo/invoice/invoice-list";
    }


    @RequestMapping(value = "/toAdd",name = "跳转到添加发票页面")
    public String toAdd(@RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "10") int size){
        //需要查询委托单数据
        PageInfo pageInfo = shippingOrderService.findByState(getCompanyId(),page,size);
        request.setAttribute("page", pageInfo);
        return "cargo/invoice/invoice-add";
    }

    @RequestMapping(value = "/toUpdate",name = "跳转到修改发票页面")
    public String toUpdate(String id) {
        Invoice invoice=invoiceService.findById(id);
        request.setAttribute("invoice",invoice);
        return "cargo/invoice/invoice-update";
    }

    @RequestMapping(value = "/edit",name = "保存数据")
    public String edit(Invoice invoice,String id){
        //判断有无id，无id进入添加
        if(StringUtils.isEmpty(invoice.getInvoiceId())) {
            invoice.setInvoiceId(id);
            //设置发票的状态  0 草稿    1 已上报
            invoice.setState(0);
            invoice.setCreateBy(getUser().getUserName());
            invoice.setCreateDept(getUser().getDeptId());
            invoice.setCreateTime(new Date());
            invoice.setCompanyId(getCompanyId());
            //查询报运单，根据报运单添加 总金额和合同号
            Export export = exportService.findById(id);
            String contractId = export.getContractIds();
            invoice.setScNo(contractId);
            String[] contractIds = contractId.split(",");
            //设置发票总金额
            Double totalMoney=0d;
            for (String cId : contractIds) {
                Contract contract = contractService.findById(cId);
                totalMoney+=contract.getTotalAmount();
            }
            invoice.setTotalMoney(totalMoney);
            invoiceService.save(invoice);
        }else {
            invoiceService.update(invoice);
        }
        return "redirect:/cargo/invoice/list.do";
    }

    @RequestMapping(value = "/cancel", name = "取消发票")
    public String cancel(String id) {
        //更新报运单的状态为已委托
        Export export = exportService.findById(id);
        export.setState(3);
        exportService.update(export);
        invoiceService.cancel(id);
        return "redirect:/cargo/invoice/list.do";
    }

    @RequestMapping(value = "/submit", name = "提交发票")
    public String submit(String id) {
        invoiceService.submit(id);
        return "redirect:/cargo/invoice/list.do";
    }

    @RequestMapping(value = "/delete", name = "删除发票")
    public String delete(String id) {
        invoiceService.deleteById(id);
        return "redirect:/cargo/invoice/list.do";
    }



}
